package com.changgou.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.changgou.framework.entity.PageResult;
import com.changgou.goods.dao.CategoryDao;
import com.changgou.goods.service.CategoryService;
import com.haha.goods.entity.Category;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author Yang
 * @date 2020/11/7
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

    @Override
    public PageResult findPage(Category category, int page, int size) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        if(!ObjectUtils.isEmpty(category)){
            //这种判断应该有好的法，现在还没找到
            queryWrapper.eq(category.getId()!=null,"id",category.getId());
            queryWrapper.like(!StringUtils.isEmpty(category.getName()),"name",category.getName());
            queryWrapper.eq(category.getGoodsNum()!=null,"goods_num",category.getGoodsNum());
            queryWrapper.eq(!StringUtils.isEmpty(category.getIsShow()),"is_show",category.getIsShow());
            queryWrapper.eq(!StringUtils.isEmpty(category.getIsMenu()),"is_menu",category.getIsMenu());
            queryWrapper.eq(category.getSeq()!=null,"seq",category.getSeq());
            queryWrapper.eq(category.getParentId()!=null,"parent_id",category.getParentId());
            queryWrapper.eq(category.getTemplateId()!=null,"template_id",category.getTemplateId());
        }
        Integer count = baseMapper.selectCount(queryWrapper);
        int offset = (page-1)*size;
        List<Category> categories = baseMapper.findPage(queryWrapper,offset,size);
        PageResult pageResult = new PageResult(count, categories);
        return pageResult;
    }

    @Override
    public List<Category> findByParentId(Integer pid) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",pid);
        return baseMapper.selectList(queryWrapper);
    }
}
